<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理<small>用户管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名/角色" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPageByCondition()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column prop="id" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column label="适用性别" align="center">
                    <template slot-scope="scope">
                        <!-- .gender-->
                        <span>{{ scope.row.gender == "1" ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="说明" align="center"></el-table-column>
                <el-table-column prop="station" label="状况" align="center"></el-table-column>
                <el-table-column prop="telephone" label="电话" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible" @close="resetField()">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form :model="formData" ref="dataAddForm" label-position="right" label-width="100px" :rules="rules" >
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名" prop="code">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码" prop="name">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="适用性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="不限" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="电话">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="状况">
                                                <el-input v-model="formData.station" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit" @close="editResetField()">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form :model="formData" ref="dataEditForm" :rules="rules" label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名" prop="code">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码" prop="name">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="适用性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="不限" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="电话">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="状况">
                                                <el-input v-model="formData.station" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            activeName:[],
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            roleIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                code: [
                    { required: true, message: '项目编码为必填项', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10个字符', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '项目名称为必填项', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10个字符', trigger: 'blur' }
                ]
            }
        },
        created() {
            //调用findPage方法，执行分页查询
            this.findPage();
        },
        methods: {
            resetField(){
                //重置校验
                this.$refs['dataAddForm'].resetFields();
            },
            findPageByCondition(){
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //编辑
            handleEdit() {

                //2 调用UserController中的更新方法
                axios.post("../user/edit.do?roleIds="+this.roleIds, this.formData).then(response => {
                    //更新失败,则提示错误信息
                    if(!response.data.flag){
                        this.$message({

                            message: response.data.message,
                            type: 'error'
                        });
                    }else{
                        //更新成功,提示成功信息
                        this.$message({

                            message: response.data.message,
                            type: 'success'
                        });
                        //关闭编辑对话框
                        this.dialogFormVisible4Edit = false;
                        //重新加载分页数据
                        this.findPage();
                    }
                })
            },
            //添加
            handleAdd() {


                //先对表单进行校验的判断
                this.$refs['dataAddForm'].validate((valid)=>{
                    if (valid){
                        //如果校验全部通过,判断是否选中了检查项
                        if(this.checkitemIds.length>0){

                            //如果选中了检查项，就发送请求，执行新增检查组
                            axios.post("/checkgroup/add.do?checkItemIds="+this.checkitemIds,this.formData).then((res)=>{

                                //关闭新增窗口
                                this.dialogFormVisible = false;

                                if (res.data.flag){
                                    //提示成功信息
                                    this.$message.success(res.data.message);
                                    //刷新页面
                                    this.findPage();
                                } else{
                                    //提示错误信息
                                    this.$message.error(res.data.message);
                                }

                            })

                        }else{
                            //如果没有选中检查项，提示错误信息
                            this.$message.error("请选择检查项!");

                        }

                    } else{
                        //提示错误信息
                        this.$message.error("表单校验失败，请重新输入！");

                    }
                })



            },
            //分页查询
            findPage() {
                //1 组装查询条件
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //2 ajax请求CheckItemController中的分页方法,携带查询条件
                axios.post("/user/findPage.do", param).then((res) => {
                    if(res.data.flag){
                        //如果查询成功，将数据绑定到数据对象中，提示成功信息
                        //将总记录数绑定到数据对象中
                        this.pagination.total = res.data.data.total;
                        //将查询到的分页的数据集合响应成的json数组绑定到数据对象中
                        this.dataList = res.data.data.rows;
                        if (this.dataList.length==0 && this.pagination.currentPage>1){
                            this.pagination.currentPage = this.pagination.currentPage - 1
                            this.findPage();
                        }

                        //提示成功信息
                        this.$message.success(res.data.message);
                    }else{
                        //如果查询失败，提示错误信息
                        this.$message.error(res.data.message);
                    }

                }).catch((r)=>{
                    this.showMessage(r);
                });
            },
            //权限不足提示
            showMessage(r){
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            // 重置表单
            resetForm() {
                //清空表单数据
                this.formData = {};
                //每次弹窗，默认选中第一个标签页
                this.activeName = 'first';
                //每次弹窗，将之前选中的所有的检查项id清空
                this.roleIds = [];
            },
            // 弹出添加窗口
            handleCreate() {
                //弹出新增对话框
                this.dialogFormVisible = true;
                this.resetForm();

                //发送请求查询所有角色信息
                axios.post("/role/findAll.do").then((res)=>{

                    if(res.data.flag){
                        //将查询出来的数据绑定到数据对象中，让其在页面中显示出来
                        this.tableData= res.data.data
                    }else{
                        //提示错误信息
                        this.$message.error(res.data.message);
                    }

                })

            },

            // 弹出编辑窗口
            handleUpdate(row) {
                //数据回显
                this.formData={...row};

                /*var id = row.id;
                //1 通过用户id获取的基本信息并进行数据绑定(this.formData)
                axios.get("/user/findById.do?id=" + id).then(response => {
                    this.formData = response.data.data;
                })*/
                //2 获取全部的角色进行绑定
                this.findAllRole();
                //3 通过用户id获取关联的角色id并进行绑定
                axios.get("/user/findRoleIdsByUserId.do?id=" + id).then(response => {
                    this.roleIds = response.data.data;
                });
                //4 弹出编辑窗口
                this.dialogFormVisible4Edit = true;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //alert(currentPage);
                //将页码进行绑定
                this.pagination.currentPage = currentPage;
                //调用分页方法
                this.findPage();
            },
            // 删除
            handleDelete(row) {

                this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // alert("点击的是确定");
                    // 1 获取要删除的检查项的id
                    var id = row.id;
                    //2 调用UserController中的删除方法
                    axios.post("../user/delete.do?id=" + id).then(response => {
                        //3 获取返回的操作结果
                        //3.1 删除失败,提示错误信息
                        if (!response.data.flag) {
                            this.$message({
                                message: response.data.message,
                                type: 'error'
                            });
                        } else {
                            //3.2 删除成功,提示成功信息,重新调用this.findPage()
                            this.$message({
                                message: response.data.message,
                                type: 'success'
                            });
                            this.findPage();
                        }
                    })

                }).catch(() => {
                    //alert("点击的是取消");
                });
            }
        }
    })
</script>
</html>
